package com.pkx.guide.holidays.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.guide.holidays.entity.THolidaysDate;
import com.pkx.guide.holidays.key.THolidaysDateKey;
import com.pkx.guide.holidays.mapper.THolidaysDateMapper;
import com.pkx.guide.holidays.service.THolidaysDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 节假日信息表服务实现类
 *
 * @Author : YangLe
 * @create 2025/8/28 13:46
 */
@Service
public class THolidaysDateServiceImpl extends ServiceImpl<THolidaysDateMapper, THolidaysDate> implements THolidaysDateService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Boolean add(THolidaysDate tHolidaysDate) {
        tHolidaysDate.setDelFlg(0);
        return save(tHolidaysDate);
    }

    @Override
    public Boolean modify(THolidaysDate tHolidaysDate) {
        return updateById(tHolidaysDate);
    }

    @Override
    public Boolean deleteById(Integer id) {
        LambdaUpdateWrapper<THolidaysDate> lambdaUpdateWrapper = new UpdateWrapper<THolidaysDate>().lambda()
                .eq(THolidaysDate::getId, id)
                .set(THolidaysDate::getDelFlg, 1);
        update(lambdaUpdateWrapper);
        redisTemplate.delete(THolidaysDateKey.KEY_HOLIDAYS_ALL);
//        System.out.println("删除redis");
        LocalDate today = LocalDate.now();
        String todayStr = today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

//            getStartTime大于等于 startOfDay getEndTime小于等于 endOfDay
        List<THolidaysDate> holidaysList = list(
                new LambdaQueryWrapper<THolidaysDate>()
                        .eq(THolidaysDate::getDelFlg, 0)
                        .apply("DATE_FORMAT(start_time, '%Y-%m-%d') <= {0}", todayStr)
                        // //        redisTemplate.opsForValue().set(THolidaysDateKey.KEY_HOLIDAYS_ALL, holidaysList);检查结束时间的年月日部分是否晚于等于今天（根据实际需求调整）
                        .apply("DATE_FORMAT(end_time, '%Y-%m-%d') >= {0}", todayStr)
        );
        System.out.println("节假日数据同步到Redis成功，共" + holidaysList.size() + "条记录");
        return true;
    }
    @Override
    public IPage<THolidaysDate> getList(Page page, THolidaysDate tHolidaysDate) {

        LambdaQueryWrapper<THolidaysDate> lambdaQueryWrapper = new QueryWrapper<THolidaysDate>().lambda();
        lambdaQueryWrapper.eq(tHolidaysDate.getId() != null, THolidaysDate::getId, tHolidaysDate.getId());
        lambdaQueryWrapper.eq(tHolidaysDate.getHolidayName() != null, THolidaysDate::getHolidayName, tHolidaysDate.getHolidayName());
        lambdaQueryWrapper.eq(tHolidaysDate.getStartTime() != null, THolidaysDate::getStartTime, tHolidaysDate.getStartTime());
        lambdaQueryWrapper.eq(tHolidaysDate.getEndTime() != null, THolidaysDate::getEndTime, tHolidaysDate.getEndTime());
        lambdaQueryWrapper.eq(tHolidaysDate.getAdjustedTime() != null, THolidaysDate::getAdjustedTime, tHolidaysDate.getAdjustedTime());
        lambdaQueryWrapper.eq(THolidaysDate::getDelFlg, 0);
        return page(page, lambdaQueryWrapper);
    }


}
